<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/iview/3.5.4/styles/iview.css" rel="stylesheet">
    <style>
        #app {
            width: 100%;
            min-height: 100vh;
            overflow: hidden;
            background-size:100% 100%;
            background-image: url('/image/login_bg.jpg');
            background-repeat: no-repeat;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .center {
            min-width: 500px;
            padding: 40px;
            background-color: #FFF;
            border-radius: 10px;
            opacity: 0.9;
            text-align: center;
        }
        .form {
            padding: 10px 0;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="center">
            <h1>登录系统</h1>
            <i-form ref="Form" class="form" :model="model" :rules="rules">
                <form-item prop="username">
                    <i-input type="text" v-model="model.username" placeholder="用户名">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </i-input>
                </form-item>
                <form-item prop="password">
                    <i-input type="password" v-model="model.password" placeholder="密码">
                        <Icon type="ios-lock-outline" slot="prepend"></Icon>
                    </i-input>
                </form-item>
                <form-item v-if="{$use_captcha}" prop="captcha">
                    <div style="display: flex; align-items: center;">
                        <i-input type="captcha" v-model="model.captcha" placeholder="验证码">
                            <Icon type="ios-lock-outline" slot="prepend"></Icon>
                        </i-input>
                        <img style="width: 150px; cursor: pointer;" :src="captchaImg" alt="captcha" @click="changeCaptchaImg" />
                    </div>
                </form-item>
                <form-item>
                    <Checkbox v-model="model.remember">7天内免登陆</Checkbox>
                    <i-button style="width: 100%;" :loading="loading" type="primary" @click="handleSubmit()">登录</i-button>
                </form-item>
            </i-form>
        </div>
    </div>
    
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/iview/3.5.4/iview.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <script src="__STATIC__/main.js"></script>
    <script>
        if(self.frameElement != null) {
            window.parent.document.location.reload(true);
        }
        new Vue({
            el: '#app',
            data: {
                loading: false,
                model: {
                    username: '',
                    password: '',
                    captcha: '',
                    remember: false,
                },
                captchaImg: '',
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码最少6位', trigger: 'blur' },
                    ],
                    captcha: [
                        { required: true, message: '请输入验证码', trigger: 'blur' }
                    ],
                }
            },
            mounted() {
                this.changeCaptchaImg()
            },
            methods: {
                changeCaptchaImg() {
                    this.captchaImg = '{:captcha_src()}?v=' + Math.random()
                },
                handleSubmit() {
                    this.$refs.Form.validate((valid) => {
                        if (!valid) return
                        this.loading = true
                        const data = {
                            ...this.model,
                            username: btoa(this.model.username),
                            password: btoa(this.model.password),
                        }
                        post('{$login_path}', data, (res) => {
                            this.loading = false
                            if(!res) return
                            window.location.href = '/admin'
                        })
                    })
                }
            }
        })
        window.$Message = Vue.prototype.$Message
    </script>
</body>
</html>